package br.com.eji.relatorio;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.NoSuchElementException;
import java.util.Scanner;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import cucumber.api.java.en.When;

public class Relatorios {

	
	private static int NumPassed = 0;
	private static int NumFailed = 0;
	private static String NomeCaso = null;
	
	public static String formataTempo(int elapsed) {
		int ss = elapsed % 60;
		elapsed /= 60;
		int min = elapsed % 60;
		elapsed /= 60;
		int hh = elapsed % 24;
		return strzero(hh) + ":" + strzero(min) + ":" + strzero(ss);
	}
	 
	private static String strzero(int n) {
		if (n < 10) {
			return "0" + String.valueOf(n);
	    }
		return String.valueOf(n);
	}
	
	public void Teste(String saidaPDF, String saidaXML) throws Throwable{
		Document doc = new Document(PageSize.A4);
		try{
			//Debug
			System.out.println("----------");
			System.out.println("----------");
			System.out.println("----------");
			System.out.println("----------");
			System.out.println("----------");
			System.out.println("----------");
			
			//Cria A documentação e abre
			
			PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(saidaPDF));
			doc.open();
			
			//Cria as fontes
			Font passed = new Font(FontFamily.TIMES_ROMAN, 9, Font.BOLD, new BaseColor(000, 102, 0));
			Font failed = new Font(FontFamily.TIMES_ROMAN, 9, Font.BOLD, new BaseColor(204, 0, 0));
			Font TesteAce = new Font(FontFamily.TIMES_ROMAN, 9, Font.BOLD, new BaseColor(0, 0, 255));
			Font grid1 = new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD, new BaseColor(0, 0, 0));
			Font grid2 = new Font(FontFamily.TIMES_ROMAN, 9, Font.BOLD, new BaseColor(0, 0, 0));
			
			//Abre o xml e faz a primeira verificação para pegar o numero de falhas
			Scanner scanner = new Scanner(new FileReader(saidaXML)).useDelimiter("\\||\\n");
			try{
				
				//Adiciona Logo			
				//Image image1 = Image.getInstance("images/logo_axxiom.png");
				//image1.scalePercent(65f);
				//image1.setAbsolutePosition(70f, 770f);
				//doc.add(image1);
				
				//Adiciona Paragrafo
				Font font1 = new Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL);
				Paragraph Topo = new Paragraph("Axxiom Soluções Tecnológicas S.A.\n   Relatório de automação de teste", font1);
				Topo.setIndentationLeft(300);
				doc.add(Topo);

				Double Tempo = 0.0;
				String TempoFinal = null;
				double doubleTempoFinal = 0.0;
				
				try {
					
				
					//Vai linha por linha procurando por erros
					while(scanner.hasNext()){
						String linha = scanner.next();
						if(linha.contains("testcase classname")){
							String LinhaCompleta = linha;
							String[] TempoArray = LinhaCompleta.split("\"");
							for(int i=0;i<TempoArray.length;i++){
								if(TempoArray[i].contains("time=")){
									i++;
									Tempo = Double.valueOf(TempoArray[i]) + Tempo;
									TempoFinal = String.valueOf(Tempo);
								}
							}
							
						}
						
						if(linha.contains("passed")){
							NumPassed++;
						}
						if(linha.contains("failed")){
							NumFailed++;
						}
						
						System.out.println("--------------");
						System.out.println("--------------");
						System.out.println(NumPassed);
						System.out.println(NumFailed);
						System.out.println("--------------");
						System.out.println("--------------");
					}
				} catch (NumberFormatException e) {
					System.out.println("NumberFormatException");
				}
				//Cria espaçamentos
				Paragraph space = new Paragraph("");
				space.setSpacingAfter(10);
				doc.add(space);
				
				
				
				String PassedStr = String.valueOf(NumPassed);
				String FailedStr = String.valueOf(NumFailed);
				String NomeDoCaso = saidaXML;
				int tamNomecaso = NomeDoCaso.length();
				String vNomeDoCaso = NomeDoCaso.substring(11, (tamNomecaso-4));
				//Trata o nome do caso				
				
				
				//Cria tabela do cabeçalho
				PdfPTable table1 = new PdfPTable(1);
				PdfPCell cell1Tb1 = new PdfPCell(new Paragraph(vNomeDoCaso));
				table1.addCell(cell1Tb1);
				doc.add(table1);
				doubleTempoFinal = Double.valueOf(TempoFinal);
				PdfPTable table = new PdfPTable(6);
				
				PdfPCell cell1 = new PdfPCell(new Paragraph("Falhas:",grid1));
	            PdfPCell cell2 = new PdfPCell(new Paragraph(FailedStr,grid2));
	            PdfPCell cell3 = new PdfPCell(new Paragraph("Sucesso:",grid1));
	            PdfPCell cell4 = new PdfPCell(new Paragraph(PassedStr,grid2));
	            PdfPCell cell5 = new PdfPCell(new Paragraph("Tempo:",grid1));
	            PdfPCell cell6 = new PdfPCell(new Paragraph(formataTempo((int)doubleTempoFinal),grid2));
	            
	            table.addCell(cell1);
	            table.addCell(cell2);
	            table.addCell(cell3);
	            table.addCell(cell4);
	            table.addCell(cell5);
	            table.addCell(cell6);
	            
	            doc.add(table);
				
	            
	            
	            
	            
				Scanner scanner2 = new Scanner(new FileReader(saidaXML)).useDelimiter("\\||\\n");
				while (scanner2.hasNext()) {
					
					String linha1 = scanner2.next();
					  
					  //Retiras linhas desnecessárias
					  linha1 = linha1.replace("<system-out>", "");
					  linha1 = linha1.replace("<![CDATA[", "");
					  
					  Paragraph p10 = new Paragraph("");
					  p10.setSpacingAfter(2);
					  doc.add(p10);
					  
					  //LINHA 1
					  if(linha1.contains("testcase classname")){
						  //Pega o nome do caso e imprime no PDF
						  String Caso1 = linha1;
						  String[] c = Caso1.split("\"");
						  for(int i=0;i<c.length;i++){
							double time1 = 0.0;
							String Tieeme1 =  null;
						  	if(c[i].contains("time=")){
						  		i++;
						  		time1 = Double.valueOf(c[i]);

						  		Tieeme1 = formataTempo((int)time1);
						  		doc.add(new Phrase("Tempo de execução: "+ Tieeme1, TesteAce));
						  	}
					  		//if(c[i].contains("TSTACC")){
					  		if((c[i].contains("classname"))||(c[i].contains("name"))){
						  		String pre = "";
						  		if (c[i].contains("classname")) pre = "Feature: ";
						  		else if (c[i].contains("name")) pre = "Cenário: ";
						  		i++;
						  		String nome1= pre+c[i];
								doc.add(new Paragraph(nome1, TesteAce));
							}
						  }
					  }
					  else if (linha1.contains("failure message")){
						  String Caso1 = linha1;
						  String[] c = Caso1.split("\"");
						  for(int i=0;i<c.length;i++){
							  if(c[i].contains("passed")){
								  String nome1= c[i];
								  
								  if (!((c[i].contains("When"))||(c[i].contains("And"))||(c[i].contains("Then"))||(c[i].contains("Given")))){
									  nome1 = c[i-2]+"\""+c[i-1]+"\""+c[i];
								  }
								  nome1 = nome1.replace(">","");
								  doc.add(new Paragraph(nome1, passed));
							  }
							  else if (c[i].contains("failed")){
								  String nome1= c[i];
								  
								  if (!((c[i].contains("When"))||(c[i].contains("And"))||(c[i].contains("Then"))||(c[i].contains("Given")))){
									  nome1 = c[i-2]+"\""+c[i-1]+"\""+c[i];
								  }
								  nome1 = nome1.replace(">","");
								  doc.add(new Paragraph(nome1, failed));
							  }
							  else if (c[i].contains("skipped")){
								  String nome1= c[i];
								  
								  if (!((c[i].contains("When"))||(c[i].contains("And"))||(c[i].contains("Then"))||(c[i].contains("Given")))){
									  nome1 = c[i-2]+"\""+c[i-1]+"\""+c[i];
								  }
								  nome1 = nome1.replace(">","");
								  doc.add(new Paragraph(nome1, grid2));
							  }
						  }
					  }  
						  //Pega o tempo do caso
					  else if(linha1.contains("passed")){
						  doc.add(new Paragraph(linha1, passed));
					  }else if(linha1.contains("failed")){
						  doc.add(new Paragraph(linha1, failed));
					  }else if(linha1.contains("skipped")){
						  doc.add(new Paragraph(linha1, grid2));
					  }else if(linha1.contains("StackTrace:")){
						  doc.add(new Paragraph(linha1, grid2));
						  linha1 = scanner2.next();
						  doc.add(new Paragraph(linha1, grid2));
					  }else{
						  Paragraph p1 = new Paragraph("");
						  doc.add(p1);
					  }
					 
					  	  
					 //LINHA 2
					  String linha2 = scanner2.next();
					  linha2 = linha2.replace("<system-out>", "");
					  linha2 = linha2.replace("<![CDATA[", "");
					  
					  doc.add(p10);
					  
					  if(linha2.contains("testcase")){
						  String Caso2 = linha2;
						  String[] d = Caso2.split("\"");
						  for(int i=0;i<d.length;i++){ 
							  	double time2 = 0.0;
							  	String Tieeme2 = null;
							  	if(d[i].contains("time=")){
							  		i++;
							  		time2 = Double.valueOf(d[i]);
							  		//Tieeme2 = String.valueOf(time2);
							  		Tieeme2 = formataTempo((int)time2);
							  		doc.add(new Phrase("Tempo de execução: "+ Tieeme2, TesteAce));
							  	}
							  	//if(d[i].contains("TSTACC")){
						  		if((d[i].contains("classname"))||(d[i].contains("name"))){
						  			String pre = "";
							  		if (d[i].contains("classname")) pre = "Feature: ";
							  		else if (d[i].contains("name")) pre = "Cenário: ";
							  		i++;
							  		String nome2= pre+d[i];
									doc.add(new Paragraph(nome2, TesteAce));
								}
							} 
					  }
					  else if (linha2.contains("failure message")){
						  String Caso2 = linha2;
						  String[] d = Caso2.split("\"");
						  for(int i=0;i<d.length;i++){
							  if(d[i].contains("passed")){
								  String nome2= d[i];
								  
								  if (!((d[i].contains("When"))||(d[i].contains("And"))||(d[i].contains("Then"))||(d[i].contains("Given")))){
									  nome2 = d[i-2]+"\""+d[i-1]+"\""+d[i];
								  }
								  nome2 = nome2.replace(">","");
								  doc.add(new Paragraph(nome2, passed));
							  }
							  else if (d[i].contains("failed")){
								  String nome2= d[i];
								  
								  if (!((d[i].contains("When"))||(d[i].contains("And"))||(d[i].contains("Then"))||(d[i].contains("Given")))){
									  nome2 = d[i-2]+"\""+d[i-1]+"\""+d[i];
								  }
								  nome2 = nome2.replace(">","");
								  doc.add(new Paragraph(nome2, failed));
							  }
							  else if (d[i].contains("skipped")){
								  String nome2= d[i];
								  
								  if (!((d[i].contains("When"))||(d[i].contains("And"))||(d[i].contains("Then"))||(d[i].contains("Given")))){
									  nome2 = d[i-2]+"\""+d[i-1]+"\""+d[i];
								  }
								  nome2 = nome2.replace(">","");
								  doc.add(new Paragraph(nome2, grid2));
							  }
						  }
					  }
					  else if(linha2.contains("passed")){
						  doc.add(new Paragraph(linha2, passed));
					  }else if(linha2.contains("failed")){
						  doc.add(new Paragraph(linha2, failed));
					  }else if(linha2.contains("skipped")){
						  doc.add(new Paragraph(linha2, grid2));
					  }else if(linha2.contains("StackTrace:")){
						  doc.add(new Paragraph(linha2, grid2));
						  linha2 = scanner2.next();
						  doc.add(new Paragraph(linha2, grid2));
					  }else{
						  Paragraph p1 = new Paragraph("");
						  doc.add(p1);
					  }
						  
					  //LINHA 3
					  String linha3 = scanner2.next();
					  linha3 = linha3.replace("<system-out>", "");
					  linha3 = linha3.replace("<![CDATA[", "");
					  
					  doc.add(p10);
					  
					  if(linha3.contains("testcase")){
						  String Caso3 = linha3;
						  String[] e = Caso3.split("\"");
						  for(int i=0;i<e.length;i++){  
							  double time3 = 0.0;
							  String Tieeme3 = null;
							  	if(e[i].contains("time=")){
							  		i++;
							  		time3 = Double.valueOf(e[i]);
							  		//Tieeme3 = String.valueOf(time3);
							  		Tieeme3 = formataTempo((int)time3);
							  		doc.add(new Phrase("Tempo de execução: "+ Tieeme3, TesteAce));
							  	}
							  //if(e[i].contains("TSTACC")){
						  		if((e[i].contains("classname"))||(e[i].contains("name"))){
						  			String pre = "";
							  		if (e[i].contains("classname")) pre = "Feature: ";
							  		else if (e[i].contains("name")) pre = "Cenário: ";
							  		i++;
							  		String nome3= pre+e[i];
									doc.add(new Paragraph(nome3, TesteAce));
								}
							} 
					  }
					  else if (linha3.contains("failure message")){
						  String Caso3 = linha3;
						  String[] e = Caso3.split("\"");
						  for(int i=0;i<e.length;i++){
							  if(e[i].contains("passed")){
								  String nome3= e[i];
								  
								  if (!((e[i].contains("When"))||(e[i].contains("And"))||(e[i].contains("Then"))||(e[i].contains("Given")))){
									  nome3 = e[i-2]+"\""+e[i-1]+"\""+e[i];
								  }
								  nome3 = nome3.replace(">","");
								  doc.add(new Paragraph(nome3, passed));
							  }
							  else if (e[i].contains("failed")){
								  String nome3= e[i];
								  
								  if (!((e[i].contains("When"))||(e[i].contains("And"))||(e[i].contains("Then"))||(e[i].contains("Given")))){
									  nome3 = e[i-2]+"\""+e[i-1]+"\""+e[i];
								  }
								  nome3 = nome3.replace(">","");
								  doc.add(new Paragraph(nome3, failed));
							  }
							  else if (e[i].contains("skipped")){
								  String nome3= e[i];
								  
								  if (!((e[i].contains("When"))||(e[i].contains("And"))||(e[i].contains("Then"))||(e[i].contains("Given")))){
									  nome3 = e[i-2]+"\""+e[i-1]+"\""+e[i];
								  }
								  nome3 = nome3.replace(">","");
								  doc.add(new Paragraph(nome3, grid2));
							  }
						  }
					  }
					  else if(linha3.contains("passed")){
						  doc.add(new Paragraph(linha3, passed));
					  }else if(linha3.contains("failed")){
						  doc.add(new Paragraph(linha3, failed));
					  }else if(linha3.contains("skipped")){
						  doc.add(new Paragraph(linha3, grid2));
					  }else if(linha3.contains("StackTrace:")){
						  doc.add(new Paragraph(linha3, grid2));
						  linha3 = scanner2.next();
						  doc.add(new Paragraph(linha3, grid2));
					  }else{
						  Paragraph p1 = new Paragraph("");
						  doc.add(p1);
					  }


					  	
					  System.out.println(linha1);
					  System.out.println(linha2);
					  System.out.println(linha3);
				}
				doc.close();
				 
			}catch(NoSuchElementException e){
				 
				doc.close();
			}
			doc.close();
		}catch(NoSuchElementException e){
			doc.close();
		}
	}
	
}


